/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.consultar;

import br.com.deployment.entidade.humana.Pessoa;
import br.com.deployment.entidade.protocolo.Protocolo;
import br.com.deployment.entidade.protocolo.ServicoProtocolo;
import br.com.deployment.entidade.selo.Selo;
import br.com.deployment.servico.PessoaServico;
import br.com.deployment.servico.ProtocoloServico;
import br.com.deployment.servico.SeloServico;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author elyjr
 */
@ManagedBean
@ViewScoped
public class ManagerConsultarProtocolo implements Serializable{

    @EJB
    ProtocoloServico protocoloServico;
    @EJB
    PessoaServico pessoaServico;
    @EJB
    SeloServico seloServico;
    
    private Protocolo protocolo;
    private List<Protocolo> protocolos;
    private Selo seloVer;
    private Integer cod;
    
    

    public ManagerConsultarProtocolo() {
        this.init();
    }
    
    public void init(){
        this.protocolo = new Protocolo();
        this.protocolo.setCliente(new Pessoa());
    }
    
    public void buscar(){
        if(cod != null){
            protocolo.setId(cod);
        }
        if(protocolo.getCliente() != null)
            protocolo.setCliente(pessoaServico.find(protocolo.getCliente().getId()));
         protocolos = protocoloServico.buscar(protocolo);
        
        for (int i = 0; i < protocolos.size(); i++) {
           protocolos.get(i).setServicoProtocolos(protocoloServico.servicoProtocolos(protocolos.get(i)));
        }
    }
    
    
    public boolean pendenteSelo(int id){
        return protocoloServico.verificaPendenciaSelo(id);
    }

    public Integer getCod() {
        return cod;
    }

    public void setCod(Integer cod) {
        this.cod = cod;
    }
    
    public boolean pendentePagamento(int id){
        return protocoloServico.verificaPendenciaPagamento(id);
    }
    
    public BigDecimal valorDoProtocolo(Protocolo protocolo){
        BigDecimal total = new BigDecimal(0);
        for( ServicoProtocolo sp :  protocolo.getServicoProtocolos()){
            total = total.add( sp.getValor());
        }
        return total;
    }

    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerConsultarProtocolo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Protocolo getProtocolo() {
        return protocolo;
    }

    public Selo getSeloVer() {
        return seloVer;
    }

    public void setSeloVer(Selo seloVer) {
        this.seloVer = seloVer;
    }
    

    public void setProtocolo(Protocolo protocolo) {
        this.protocolo = protocolo;
    }

    public List<Protocolo> getProtocolos() {
        return protocolos;
    }

    public void setProtocolos(List<Protocolo> protocolos) {
        this.protocolos = protocolos;
    }
    
     public void protocoloGetSelos(int id){
        seloVer = new Selo();
        seloVer = seloServico.find(id);
    }
    
    
}